<template>
  <div>
    <div v-if="email_data && email_data.EmailTypeData">
      <email-item
        v-for="(item, index) in email_data.EmailTypeData"
        :key="index"
        :data="item"
        :count="unread_count(item.typeId)"
        :message="getMessage(item.typeId)"
        :params="item.typeId"
    ></email-item>
    </div>
  </div>
</template>
<script>
import EmailItem from "./EmailItem";
export default {
  name: "EmailBody",
  components: {
    EmailItem,
  },
  created() {
    this.getEmail();
  },
  data() {
    return {
      email_data:null,// 邮件信息
    };
  },
  methods: {
    // 请求配置数据
    async getEmail() {
      const { data: res } = await this.$http.get("emailType");
      this.email_data = res.data;
      console.log(res);
    },
  },
  computed: {
    // 计算未读条数
    unread_count(){
      return function (id) {
        // 去数组里查找该ID对应的下标
        let email_type = this.email_data.email_type;// 邮件种类
        let email_count = this.email_data.email_count;// 邮件种类 对应的数量
        for (let index = 0; index < email_type.length; index++) {
          if((email_type[index]) - 0 == (id) - 0){
            return email_count[index];
          }
        }
      }
    },
    // 获取当前显示的消息
    getMessage(){
      return function (id){
        // 去数组里查找该ID对应的下标
        let email_type = this.email_data.email_type;// 邮件种类
        let email_message = this.email_data.email_message;
        for (let index = 0; index < email_type.length; index++) {
          if((email_type[index]) - 0 == (id) - 0){
            return email_message[index];
          }
        }
      }
    }
  },
};
</script>
<style lang="less" scoped>
</style>